package com.eims.sls_app.ui;

import org.json.JSONObject;

import com.android.volley.toolbox.RequestParams;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.LotteryUtils;
import com.eims.sls_app.view.MyToast;

import bls.juejin.cn.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SunActivity extends MyBaseActivity implements OnClickListener {
	private static final String TAG = "SunActivity";
	private int schemeId;
	private EditText et_content;
	private Button btn_cancel,btn_confirm;
	private String opt, auth, info, time, imei, crc; // 格式化后的参数
	private Context context;
	private MyHandler mHandler;
	private String context_Sun;
	private MyAsynTask mMyAsynTask;
	private String max_jiangjin;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sun);
		schemeId = getIntent().getIntExtra("schemeId", 0);
		max_jiangjin = getIntent().getStringExtra("max_jiangjin");
		MyToast.getToast(this, schemeId+"").show();
		mHandler = new MyHandler();
		initView();
		initlist();
		
	}
	private void initlist() {
		btn_cancel.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);
		
	}
	private void initView() {
		et_content = (EditText) findViewById(R.id.sd_et_content);
		btn_cancel = (Button) findViewById(R.id.sd_btn_cancel);
		btn_confirm = (Button) findViewById(R.id.sd_btn_confirm);
		
		
	}
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.sd_btn_cancel:
			this.finish();
			break;
		case R.id.sd_btn_confirm:
			
			context_Sun = et_content.getText().toString().trim();
			mMyAsynTask = new MyAsynTask();
			mMyAsynTask.execute();
			backToHall();
			//this.finish();
			break;

		default:
			break;
		}
		
	}
	
	public void backToHall() {
		Intent intent = new Intent(getApplicationContext(), MainActivity.class);
		this.startActivity(intent);
		this.finish();
	}
	
	
	class MyAsynTask extends AsyncTask<Integer, Integer, String> {
		
		String error = "-1";

		@Override
		protected String doInBackground(Integer... params) {
			
				opt = "85";
				time = RspBodyBaseBean.getTime();
				imei = RspBodyBaseBean.getIMEI(context);
				//info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);
				//info = RspBodyBaseBean.change_TheSun2(schemeId+"", context_Sun, max_jiangjin);
				//info = RspBodyBaseBean.change_comment(22+"", context_Sun);
				//info = 8+"";
				//info = LotteryUtils.Long2DateStr_detail(System.currentTimeMillis());
				// String key = MD5.md5(App.user.getUserPass() + App.MD5_key);
				String key = App.key;
				crc = RspBodyBaseBean.getCrc(time, imei, key, info,
						App.user.getUid());
				auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
				String[] values = { opt, auth, info };
				Log.i(TAG, "晒单"+RequestParams.convertParams(context, opt, info));
				String result = HttpUtils.doPost(App.names, values,
						App.path);
				System.out.println("晒单：" + result);
				if (result.equals("success")) {
					error = "0";
				}else {
					error = "1";
				}
			
			return error;
		}

		@Override
		protected void onPostExecute(String result) {
			mHandler.sendEmptyMessage(Integer.parseInt(result));
			super.onPostExecute(result);
		}
	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			
			switch (msg.what) {
			case 0:
				//MyToast.getToast(context, "晒单成功").show();
				break;
			case 1:
				//MyToast.getToast(context, "重复晒单").show();
				break;
			case -2:
				
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	}

}
